
// 生成环境
const baseUrl = "https://www.clarion-huodonghao.com/pump1";
// 测试环境
// const baseUrl = 'https://ticketxcx.greentoyellow.com/pump'
let prevUrl = ''
let isRequesting = false

function _get(url, success, fail, setting = {}) {
  if (!setting.noLoading) wx.showLoading({
    title: '正在加载',
  })
  if (setting.once && url === prevUrl && isRequesting) {
    return
  }
  prevUrl = url
  console.log("------start---_get----");
  isRequesting = true
  wx.request({
    url: baseUrl + url,
    header: {
      'Content-Type': 'application/json'
    },
    complete: () => {
      prevUrl = ''
      isRequesting = false
      wx.hideLoading()
    },
    success: (res) => {
      success(res.data, res.header);
    },
    fail: function (res) {
      fail(res);
    }
  });

  console.log("----end-----_get----");
}

// function ajax(url, success, fail, setting = {}) {
//   if (!setting.noLoading) wx.showLoading({
//     title: '正在加载',
//   })
//   console.log("------start---_get----");
//   wx.request({
//     url: url,
//     header: {
//       'Content-Type': 'application/json'
//     },
//     success: function (res) {
//       success(res.data, res.header);
//     },
//     fail: function (res) {
//       fail(res);
//     },
//     complete: () => {
//       wx.hideLoading()
//     }
//   });

//   console.log("----end-----_get----");
// }


/**
 * url 请求地址
 * success 成功的回调
 * fail 失败的回调
 */
function _post_from(url, data, success, fail, setting ={}) {
  if (!setting.noLoading) wx.showLoading({
    title: '正在加载',
  })
  if (setting.once && url === prevUrl && isRequesting) return
  prevUrl = url
  console.log("----_post--start-------");
  isRequesting = true
  wx.request({
    url: baseUrl + url,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    method: 'POST',
    data: { data: data },
    complete: () => {
      prevUrl = ''
      isRequesting = false
      wx.hideLoading()
    },
    success: function (res) {
      success(res.data, res.header);
    },
    fail: function (res) { 
      fail(res);
    }
  });
  console.log("----end-----_get----");
}

/**
* url 请求地址
* success 成功的回调
* fail 失败的回调
*/
function _post_json(url, data, success, fail, setting = {}) {
  if (!setting.noLoading) wx.showLoading({
    title: '正在加载',
  })
  if (setting.once && url === prevUrl && isRequesting) return
  prevUrl = url
  console.log("----_post--start-------");
  isRequesting = true
  wx.request({
    url: baseUrl + url,
    header: {
      'content-type': 'application/json',
    },
    method: 'POST',
    data: data,
    complete: () => {
      prevUrl = ''
      isRequesting = false
      wx.hideLoading()
    },
    success: function (res) {
      success(res.data, res.header);
    },
    fail: function (res) {
      fail(res);
    }
  });

  console.log("----end----_post-----");
}
module.exports = {
  _get: _get,
  _post_json: _post_json
}